<?php
/*
 * @Author: your name
 * @Date: 2020-10-29 16:33:39
 * @LastEditTime: 2020-10-30 09:51:35
 * @LastEditors: Please set LastEditors
 * @Description: 官方规则
 * @FilePath: /v3/application/adminapiv3/service/NormalRule.php
 */

namespace app\adminapiv3\service;


use app\common\service\HostNormalRuleRelation;

class NormalRule extends Base
{
    /**
     * @name 列表
     * @param $host_id
     * @param $group_id
     * @param $params
     * @param int $limit
     * @return array
     * @author wx
     * @date 2019/11/12 14:52
     */
    function list($host_id, $group_id, $params, $limit = 10)
    {
        $server = new \app\common\service\NormalRule();
        $list = $server->list($host_id, $group_id, $params, $limit);

        return $list;
    }

    /**
     * @name 修改
     * @param $id
     * @param $params
     * @return int|string
     * @author wx
     * @date 2019/11/12 14:54
     */
    function modify($host_id, $id, $params)
    {
        $this->checkOptionHost($host_id);

        $server = new HostNormalRuleRelation();
        $condition[] = array('id', 'in', $id);
        $res = $server->modify($condition, $params);

        return $res;
    }

    /**
     * @name: 批量修改规则处理方式
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-09-27 17:28:06
     */
    function bulkModifyHandle ($input)
    {

        db('HostNormalRuleRelation')->whereIn('id', $input['rule_id'])->update(['status' => $input['status'], 'handle' => $input['handle']]);

        $used_count = db('HostNormalRuleRelation')->where('host_id', $input['host_id'])->where('group_id', $input['group_id'])->where('status', 1)->count();

        $update = [
            'used' => $used_count,
            'status' => empty($used_count) ? 0 : 1,
        ];

        db('HostNormalRuleGroupRelation')->where('host_id', $input['host_id'])->where('group_id', $input['group_id'])->update($update);
    }

}